package com.itheima.reggie.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * 短信发送工具类
 * @author GRS
 * @since 2024/4/26 下午9:59
 */
@Slf4j
public class SMSUtils
{
    /**
     * 发送短信
     */
    public static void sendMessage(
        String signName,
        String templateCode,
        String phoneNumbers,
        String param
    )
    {
        DefaultProfile profile = DefaultProfile.getProfile(
            "cn-chengdu",
            "***",
            "***"
        );
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-chengdu");
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":\"" + param + "\"}");
        try
        {
            SendSmsResponse response = client.getAcsResponse(request);
            log.info("短信发送成功");
        }
        catch (ClientException e)
        {
            log.error("短信发送失败, {}", e.getMessage());
        }
    }
}